package deviceinfo.wsm.com.deviceinfo.device;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.location.LocationManager;
import android.os.Build;
import android.os.SystemClock;
import android.provider.Settings;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.UUID;

import deviceinfo.wsm.com.deviceinfo.WsmDeviceInfoSDK;
import deviceinfo.wsm.com.deviceinfo.plugin.com.an.deviceinfo.device.model.Device;
import deviceinfo.wsm.com.deviceinfo.screen.ScreenInfoUtils;

import static android.content.Context.LOCATION_SERVICE;
import static android.content.Context.SENSOR_SERVICE;

/**
 * Created by 王家宝 on 2018/7/5.
 * TEL:18241150668
 * MAIL:639586819@qq.com
 */

public class DeviceInfoUtils {

    public static LinkedHashMap<String, String> getAll() {
        Device device = new Device(WsmDeviceInfoSDK.getWsmSDK().getAppContext());
        LinkedHashMap<String, String> data = ScreenInfoUtils.getAll();
        data.put("Manufacturer", device.getManufacturer());
        data.put("Model", device.getModel());
        data.put("BuildVersionCodeName", device.getBuildVersionCodeName());
        data.put("ReleaseBuildVersion", device.getReleaseBuildVersion());
        data.put("Product", device.getProduct());
        data.put("Fingerprint", device.getFingerprint());
        data.put("Hardware", device.getHardware());
        data.put("RadioVersion", device.getRadioVersion());
        data.put("Device", device.getDevice());
        data.put("Board", device.getBoard());
        data.put("DisplayVersion", device.getDisplayVersion());
        data.put("BuildBrand", device.getBuildBrand());
        data.put("BuildHost", device.getBuildHost());
        data.put("BuildTime", device.getBuildTime() + "");
        data.put("BuildUser", device.getBuildUser());
        data.put("Serial", device.getSerial());
        data.put("OsVersion", device.getOsVersion());
        data.put("Language", device.getLanguage());
        data.put("SdkVersion", device.getSdkVersion() + "");
        data.put("开机时间", bootTime() + "");
        data.put("是否root", isRoot() + "");
        data.put("GPS是否开启", isGPSOpen() + "");
        data.put("屏幕亮度", getSystemBrightness() + "");
        data.put("是否是模拟器", isSimulator() + "");

        return data;
    }


    public static Boolean isSimulator() {
        SensorManager sensorManager = (SensorManager) WsmDeviceInfoSDK.getWsmSDK().getAppContext()
                .getSystemService(SENSOR_SERVICE);
        Sensor sensor8 = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); //光
        if (null == sensor8) {
            return true;
        } else {
            return false;
        }
    }

    private static int getSystemBrightness() {
        int systemBrightness = 0;
        try {
            systemBrightness = Settings.System.getInt(WsmDeviceInfoSDK.
                    getWsmSDK().getAppContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return systemBrightness;
    }

    public static boolean isGPSOpen() {
        //得到系统的位置服务，判断GPS是否激活
        LocationManager lm = (LocationManager) WsmDeviceInfoSDK.getWsmSDK().getAppContext().getSystemService(LOCATION_SERVICE);
        return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    //判断手机是否root
    private static boolean isRoot() {
        String binPath = "/system/bin/su";
        String xBinPath = "/system/xbin/su";

        if (new File(binPath).exists() && isCanExecute(binPath)) {
            return true;
        }
        return new File(xBinPath).exists() && isCanExecute(xBinPath);
    }

    private static boolean isCanExecute(String filePath) {
        java.lang.Process process = null;
        try {
            process = Runtime.getRuntime().exec("ls -l " + filePath);
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String str = in.readLine();
            if (str != null && str.length() >= 4) {
                char flag = str.charAt(3);
                if (flag == 's' || flag == 'x')
                    return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (process != null) {
                process.destroy();
            }
        }
        return false;
    }

    public static long bootTime() {
        return System.currentTimeMillis() - SystemClock.elapsedRealtimeNanos() / 1000000;
    }

    public static String getUniquePsuedoID() {
        String serial = null;

        String m_szDevIDShort = "35" +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +

                Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +

                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +

                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +

                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +

                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +

                Build.USER.length() % 10; //13 位

        try {
            serial = android.os.Build.class.getField("SERIAL").get(null).toString();
            //API>=9 使用serial号
            return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        } catch (Exception exception) {
            //serial需要一个初始化
            serial = "serial"; // 随便一个初始化
        }
        //使用硬件信息拼凑出来的15位号码
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();

    }


}
